class LinkedList {
    constructor() {
        this.head = null;
        this.length = 0;
    }
    append = (data) => {
        let node = new Node(data)
        if (this.length == 0) {
            this.head = node;
            this.length++;
        } else {
            let current = this.head
            while (current.next) {
                current = current.next;
            }
            current.next = node;
            this.length++
        }
    }
    insert = (index, data) => {
        let node = new Node(data)
        let currentIndex = 0;
        let current = this.head;
        try {
            if (index > this.length) {
                throw ('index not found')
            }
            while (currentIndex < index) {
                current = current.next;
                currentIndex++;
            }
            node.next = current.next//在第index个后面插入元素
            current.next = node
        } catch (err) {
            console.error(err);
        }

    }
    removeAt = (index) => {
        let currentIndex = 0;
        let current = this.head;
        try {
            if (index > this.length) {
                throw ('index not found')
            }
            while (currentIndex < index) {
                current = current.next;
                currentIndex++;
            }
            current.next = current.next.next
        } catch (err) {
            console.error(err);
        }
    }
    push = () => {
        this.head = this.head.next
    }
    update = (index, data) => {
        let node = new Node(data)
        let currentIndex = 0;
        let current = this.head;
        let pre = this.head;
        try {
            if (index > this.length) {
                throw ('index not found')
            }
            if (index > 0) {
                while (currentIndex < index - 1) {
                    current = current.next;
                    if ((currentIndex - 1) >= 0) {
                        pre = pre.next
                    }
                    currentIndex++;
                }
                node.next = current.next//在第index个后面插入元素
                pre.next = node
            }
            node.next = this.head.next
            this.head = node
        } catch (err) {
            console.error(err);
        }
    }
    toString = () => {
        let current = this.head
        let listString = '';
        while (current) {
            listString += current.data + ' ';
            current = current.next;
        }
        return listString
    }
    indexOf = (data) => {
        let current = this.head;
        let currentIndex = 0;
        let isFound = false
        try {
            while (currentIndex < this.length&&!(current.data == data)) {
                current = current.next
                currentIndex++
                if (currentIndex==this.length) {
                    throw("error")
                }
            }
        } catch (err) {
            return -1
        }

        return currentIndex;
    }
}

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}